/*
	File: C:/works/3d/games/mgt/panzer_2010/data/scripts/game_item.nut
	Author: Astrofra
*/

/*!
	@short	GameItem
	@author	Astrofra
*/

//	Use the GameItem class for items that interact with the player : 
//	Bonus, keys, doors ...
//	This class should be used as a parent class, using the "extends" keyword.
//	If it parents a trigger "item_trigger", the item will be able
//	to invoke the proper method 'PlayerEntersItem()'. Feel free to overload this method.
//	The other purpose of this class is to keep track of the state of the item,
//	when the player exists a room.

//--------------
class	GameItem
//--------------
{

	item_trigger		=	0
	inside_trigger		=	false

	enabled				=	true

	project_script		=	0

	tank				=	0
	tank_script			=	0

	/*!
		@short	OnUpdate
		Called during the scene update, each frame.
	*/
	function	OnUpdate(item)
	{
		if (!enabled)
			return

		if (tank == 0)
		{
			tank = SceneFindItem(ItemGetScene(item), "tank")
			tank_script = ItemGetScriptInstanceFromClass(tank, "SteamTank")
		}
		else
		{
			if (item_trigger != 0)
			{
				if (TriggerTestItem(item_trigger, tank))
				{
					PlayerIsInsideItem(item)

					if (!inside_trigger)
					{
						print("GameItem::OnUpdate() tank inside")
						PlayerEntersItem(item)
						inside_trigger = true
					}
				}
				else
					inside_trigger = false
			}
		}
	}

	function	PlayerEntersItem(item)
	{
		print("GameItem::PlayerEntersItem() - Stub method called.")
	}

	function	PlayerIsInsideItem(item)
	{
		//print("GameItem::PlayerIsInsideItem() - Stub method called.")
	}

	/*!
		@short	OnSetup
		Called when the item is about to be setup.
	*/
	function	OnSetup(item)
	{
		print("GameItem::OnSetup(" + ItemGetName(item) + ")")

		local	_item_trigger
		_item_trigger = ItemGetChild(item, "item_trigger")
		if (ObjectIsValid(_item_trigger))
			item_trigger = ItemCastToTrigger(_item_trigger)

		project_script = ProjectGetScriptInstance(g_project)
		enabled = project_script.GetItemStateEnabled(item)

		if (enabled)
			Enable(item)
		else
			Disable(item)
	}

	function	Enable(item)
	{
		print("GameItem::Enable(" + ItemGetName(item) + ")")

		ItemActivateHierarchy(item, true)
		enabled	=	true
		project_script.SetItemStateEnabled(item, true)
	}

	function	Disable(item)
	{
		print("GameItem::Disable(" + ItemGetName(item) + ")")

		ItemActivateHierarchy(item, false)
		enabled	=	false
		project_script.SetItemStateEnabled(item, false)

		local	_pos = ItemGetPosition(item)
		_pos.y = -5000.0
		ItemSetPosition(item, _pos)
		ItemSetPhysicPosition(item, _pos)
	}
}

//----------------------------------------
class	GameItemWithLight extends GameItem
//----------------------------------------
{
	light_cell			=	0	
	light_oscillation	=	0.0
	phase				=	0.0

	function	OnUpdate(item)
	{
		base.OnUpdate(item)

		light_oscillation += DegreeToRadian(5.0) * 30.0  * g_dt_frame
		LightSetDiffuseIntensity(light_cell, (2.0 + sin(light_oscillation + phase)) * 0.75)
		LightSetSpecularIntensity(light_cell, (2.0 + sin(light_oscillation + phase)) * 0.75)
	}

	function	OnSetup(item)
	{
		base.OnSetup(item)
		light_cell = ItemCastToLight(ItemGetChild(item, "cell_light"))
		phase = DegreeToRadian(Rand(0.0, 90.0))
	}
}

//---------------------------------------------
class	PowerCellItem extends GameItemWithLight
//---------------------------------------------
{

	function	OnUpdate(item)
	{
		base.OnUpdate(item)
	}

	function	PlayerEntersItem(item)
	{
		print("PowerCellItem::PlayerEntersItem()")
		tank_script.EnergyUp()
		Disable(item)
	}

	function	OnSetup(item)
	{
		base.OnSetup(item)
	}

}

//----------------------------------------------
class	ShieldCellItem extends GameItemWithLight
//----------------------------------------------
{

	function	OnUpdate(item)
	{
		base.OnUpdate(item)
	}

	function	PlayerEntersItem(item)
	{
		print("ShieldCellItem::PlayerEntersItem()")
		tank_script.ShieldUp()
		Disable(item)
	}

	function	OnSetup(item)
	{
		base.OnSetup(item)
	}

}

//---------------------------------------
class	KeyItem extends GameItemWithLight
//---------------------------------------
{

	key_name	=	0

	function	OnUpdate(item)
	{
		base.OnUpdate(item)
	}

	function	PlayerEntersItem(item)
	{
		print("KeyItem::PlayerEntersItem()")
		project_script.inventory.AddItem(key_name)
		project_script.hud_ui.UpdateInventory(project_script.inventory)
		project_script.audio.player.PlaySfxGotKey()
		Disable(item)
	}

	function	OnSetup(item)
	{
		base.OnSetup(item)
		key_name = ItemGetName(item)
		print("KeyItem::OnSetup() : key_name = " + key_name)
	}
}
